﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using UsedBookTradeSystem.DBContext;
using UsedBookTradeSystem.Models.Entity;

namespace UsedBookTradeSystem.Controllers.BackendControllers
{
    public class CategoryController : Controller
    {
        private readonly SystemDbContext _ctx;

        public CategoryController(SystemDbContext ctx)
        {
            _ctx = ctx;
        }
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Index()
        {
            var category = await _ctx.BookCategories.ToListAsync();
            ViewBag.Category = category;
            return View();
        }
        [Authorize(Roles = "Admin")]
        public IActionResult Add()
        {
            return View();
        }

        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Edit(int id)
        {
            var category = await _ctx.BookCategories.FirstOrDefaultAsync(c => c.Id == id);
            ViewBag.Category = category;
            return View();
        }

        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> AddCategory(BookCategory category)
        {
            if (await _ctx.BookCategories.CountAsync(x => x.CategoryName == category.CategoryName) > 0)
            { 
                TempData["OperateResult"] = "已经存在该类别！";
                return RedirectToAction(nameof(Add));
            }
            else 
            {
                await _ctx.BookCategories.AddAsync(category);
                await _ctx.SaveChangesAsync();
            }
            return RedirectToAction(nameof(Index));
        }
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> UpdateCategory(BookCategory category)
        {
            if (await _ctx.BookCategories.CountAsync(x => x.CategoryName == category.CategoryName) > 0)
            {
                TempData["OperateResult"] = "已经存在该类别！";
                return RedirectToAction(nameof(Edit), new { id = category.Id });
            }
            else
            {
                var UpdateCategory = await _ctx.BookCategories.Where(x => x.Id == category.Id).FirstAsync();
                UpdateCategory.CategoryName = category.CategoryName;
                _ctx.BookCategories.Update(UpdateCategory);
                await _ctx.SaveChangesAsync();
            }
            return RedirectToAction(nameof(Index));
        }

        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Delete(int id)
        {
            var category = await _ctx.BookCategories.Include(x => x.BookList).ThenInclude(p => p.Books).FirstOrDefaultAsync(c => c.Id == id);
            if (category == null)
            {
                TempData["OperateResult"] = "类别不存在！";
            }
            else
            {
                category.IsDeleted = true;
                if (category.BookList != null)
                {
                    foreach (var bookinfo in category.BookList)
                    {
                        bookinfo.Inventory = 0;
                        bookinfo.IsDeleted = true;
                        bookinfo.UpdatedTime = DateTime.Now;
                        if (bookinfo.Books != null)
                        {
                            foreach (var book in bookinfo.Books)
                            {
                                book.UpdatedTime = DateTime.Now;
                                book.IsDeleted = true;
                            }
                        }
                    }
                }
                _ctx.BookCategories.Update(category);
                _ctx.SaveChanges();
            }
            return RedirectToAction(nameof(Index));
        }
    }
}
